package com.briup.labs.controller;

import com.briup.labs.pojo.*;
import com.briup.labs.service.SmartDeviceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/device")
public class deviceController {

    @Autowired
    private SmartDeviceService smartDeviceService;

    @GetMapping("/by/condition")
    public Result<PageBean<SmartDevice>> list(String pageNum,
                                              String pageSize,
                                              @RequestParam(required = false) String key,
                                              @RequestParam(required = false) String typeId) {
        PageBean<SmartDevice> pageBean
                = smartDeviceService.list(pageNum, pageSize, key, typeId);
        if (pageBean.getTotal() != null && pageBean.getItems() != null) {
            List<SmartDevice> devices = smartDeviceService.list(key, typeId);
            pageBean.setTotal((long) devices.size());
            return Result.success(pageBean);
        } else {
            return Result.error("输出传入格式错误");
        }
    }

    @PostMapping("/saveOrUpdate")
    public Result saveOrUpdate(@RequestBody SmartDevice smartDevice) {
        smartDeviceService.saveOrUpdate(smartDevice);
        return Result.success();
    }

    @DeleteMapping("/{id}")
    public Result delete(@PathVariable("id") String id) {
        smartDeviceService.delete(id);
        return Result.success();
    }

}
